<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>非父组件传值</title>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
		<style>
			.sbling {
				width: 600px;
				height: 200px;
				border: 1px solid black;
				background-color: pink;
				margin-top:20px;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<haoren-component></haoren-component>
			<jerry-component></jerry-component>
		</div>
		<script>
			//1.提供事件中心
			var hub=new Vue();
			
			Vue.component('haoren-component', {
				data: function(){
					return {
						num: 0
					}
				},
				template:`<div class='sbling'>
				           <h1>haoren-component</h1>
						   <h3>{{num}}</h3>
						   <button @click='handle'>单击</button>
			         	</div>`,
				methods:{
					handle(){
						//通过事件中心,自定义一个jerry-event的事件
						hub.$emit('jerry-event',3);
					}
				}		
			});
			
			Vue.component('jerry-component', {
				data: function(){
					return {
						num: 0
					}
				},
				template:`<div class='sbling'>
				           <h1>jerry-component</h1>
						   <h3>{{num}}</h3>
						   <button>单击</button>
			         	</div>`,
				mounted:function(){
					//监听jerry-event的事件
					//let _this=this;
					hub.$on('jerry-event',(val)=>{
						console.log(val,this);
						this.num+=val;
					})
				}		
			});

			//1. 实例化一个Vue的实例
			let vm = new Vue({
				el: '#app',
				data: {

				}
			});
		</script>
	</body>
</html>
